package hjt.com.module.main.activity;

import android.content.DialogInterface;
import androidx.databinding.DataBindingUtil;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.tong.lib.base.BaseActivity;

import hjt.com.module.main.R;
import hjt.com.module.main.databinding.ActivityWebviewBinding;
import hjt.com.module.main.utils.MyJavascriptInterface;

public class WebviewActivity extends BaseActivity {

    private ActivityWebviewBinding mBindg;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initDataBinding(int layoutRes) {
        super.initDataBinding(layoutRes);
        mBindg = DataBindingUtil.setContentView(this, layoutRes);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        WebSettings webSettings = mBindg.webview.getSettings();

        // 设置与Js交互的权限
        webSettings.setJavaScriptEnabled(true);
        // 设置允许JS弹窗
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        mBindg.webview.addJavascriptInterface(new MyJavascriptInterface(getContext()),"test");
        // 先载入JS代码
        // 格式规定为:file:///android_asset/文件名.html
        mBindg.webview.loadUrl("file:///android_asset/javascript.html");


        mBindg.tvShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 通过Handler发送消息
                mBindg.webview.post(new Runnable() {
                    @Override
                    public void run() {

                        // 注意调用的JS方法名要对应上
                        // 调用javascript的callJS()方法
                        mBindg.webview.loadUrl("javascript:callAndroid()");
                    }
                });

            }
        });

        // 由于设置了弹窗检验调用结果,所以需要支持js对话框
        // webview只是载体，内容的渲染需要使用webviewChromClient类去实现
        // 通过设置WebChromeClient对象处理JavaScript的对话框
        //设置响应js 的Alert()函数
        mBindg.webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                AlertDialog.Builder b = new AlertDialog.Builder(getContext());
                b.setTitle("Alert");
                b.setMessage(message);
                b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.confirm();
                    }
                });
                b.setCancelable(false);
                b.create().show();
                return true;
            }

        });

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected boolean isDataBinding() {
        return true;
    }
}
